﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CombineProto
{
    public class MessageEnum
    {
        public string name;
        public List<EnumValue> dots = new List<EnumValue>();

        public override string ToString()
        {
            string str = "";
            if (dots.Count>0)
            {
                str = "enum " + name + " {";
                for (int i = 0; i < dots.Count; i++)
                {
                    str += "\n" + dots[i].name + " = " + dots[i].value + ";";
                }
                str += "\n}";
            }
            return str;
        }
        public void RemoveGreater(int value)
        {
            for (int i = 0; i < dots.Count; i++)
            {
                int par = int.Parse(dots[i].value);
                if (par > value)
                {
                    dots.RemoveAt(i);
                    i--;
                }
            }
        }
        public void RemoveLower(int value)
        {
            for (int i = 0; i < dots.Count; i++)
            {
                int par = int.Parse(dots[i].value);
                if (par < value)
                {
                    dots.RemoveAt(i);
                    i--;
                }
            }
        }
        public string ToLuaCmd()
        {
            string ret = "local p={\n";
            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[" + (i + 1) + "] = package..\"." + dot.msgName + "\"";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n}\nreturn {\n  id={\n";
            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[" + dot.value + "] = p[" + (i + 1) + "]";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n},\n  msg={\n";

            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[\"" + dot.msgName + "\"] = p[" + (i + 1) + "]";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }
            ret += "\n},\n msgToId={\n";

            for (int i = 0; i < dots.Count; i++)
            {
                var dot = dots[i];
                ret += "[\"" + dot.msgName + "\"] = " + dot.value + "";
                if (i < dots.Count - 1)
                {
                    ret += ",\n";
                }
            }

            ret += "\n}\n}";
            return ret;
        }
    }
}
